package com.resou.reader.view.booklebel.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.Toast;

import com.google.gson.internal.LinkedTreeMap;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.utils.GsonUtils;
import com.resou.reader.view.booklebel.LabelBean;
import com.resou.reader.view.booklebel.LabelNameBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 标签显示的适配器
 * CreateTime: 2019-03 -23
 **/
public class TypeLabelAdapter extends RecyclerView.Adapter<TypeLabelAdapter.ViewHolder> {
    private static final String TAG = "TypeLabelAdapter";
    Context context;
    List<BookLabelBean.FilterDetailBean> list;  //数据源 显示的标签
    ILabelResultCallBack iLabelResultCallBack; //点击标签的接口回调
    int NUM = 3;//最大超出数量
    int checkNum = 0;

    List<BookLabelBean.FilterDetailBean> filterDetailBeans; //选中的标签自己和

    /**
     * 点击回调
     *
     * @param iLabelResultCallBack
     */
    public void setiLabelResultCallBack(ILabelResultCallBack iLabelResultCallBack) {
        this.iLabelResultCallBack = iLabelResultCallBack;
    }

    public TypeLabelAdapter(Context context, List<BookLabelBean.FilterDetailBean> list) {
        this.context = context;
        this.list = list;
        filterDetailBeans = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_label, viewGroup, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        viewHolder.cbLabel.setText(list.get(position).getTypeDesc());
        if (list.get(position).isCheck()) {
            viewHolder.cbLabel.setChecked(true);
        } else {
            viewHolder.cbLabel.setChecked(false);
        }
        viewHolder.cbLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != iLabelResultCallBack) {
                    if (viewHolder.cbLabel.isChecked()) {
                        list.get(position).setCheck(false);
                        checkNum();
                        iLabelResultCallBack.setOnclickCancellCallBack(list.get(position), filterDetailBeans);
                    } else {
                        list.get(position).setCheck(true);
                        setAllAndOther(list.get(position).getTypeDesc());
                        checkNum();
                        if (checkNum > NUM) {
                            list.get(position).setCheck(false);
                            Toast.makeText(context, "最多选择三个标签", Toast.LENGTH_SHORT).show();
                        } else {
                            iLabelResultCallBack.setOnclickReselutCallBack(list.get(position), filterDetailBeans);
                        }
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CheckedTextView cbLabel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cbLabel = itemView.findViewById(R.id.cbLabel);
        }
    }

    /**
     * 数据更新
     *
     * @param list
     */
    public void updataData(List<BookLabelBean.FilterDetailBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     *
     */
    public void clear() {

        if (this.list != null) {
            this.list.clear();
            this.filterDetailBeans.clear();
            this.notifyDataSetChanged();
        }
    }

    /**
     * 查出选中的数量
     *
     * @return
     */
    private synchronized int checkNum() {
        checkNum = 0;
        if (null != filterDetailBeans) {
            filterDetailBeans.clear();
        }
        for (BookLabelBean.FilterDetailBean filterDetailBean : list)
            if (filterDetailBean.isCheck()) {
                Log.i(TAG, "checkNum: " + filterDetailBean.getTypeDesc());
                BookLabelBean filterDetailBean1 = GsonUtils.fromJson(filterDetailBean.getTypeChild().toString(), BookLabelBean.class);
                if (filterDetailBean1.getFilterDetail() != null) {
                    for (int i = 0; i < filterDetailBean1.getFilterDetail().size(); i++) {
                        if (null != filterDetailBean1.getFilterDetail().get(i)) {
                            filterDetailBeans.add(filterDetailBean1.getFilterDetail().get(i));
                        }
                    }
                }
                checkNum++;

            }
        return checkNum;
    }

    /**
     * 如果选中全部  则将其他的标签取消
     * 否则 将 全部 取消选中
     *
     * @param label
     */
    private void setAllAndOther(String label) {
        if (label.equals("全部")) {
            if (iLabelResultCallBack != null) {
                iLabelResultCallBack.setOnclickAllCallBack();
            }
            for (int i = 0, length = list.size(); i < length; i++) {
                if (list.get(i).getTypeDesc().equals("全部")) {
                    list.get(i).setCheck(true);
                } else {
                    list.get(i).setCheck(false);
                }
            }
        } else {
            for (int i = 0, length = list.size(); i < length; i++) {
                if (list.get(i).getTypeDesc().equals("全部")) {
                    list.get(i).setCheck(false);
                }
            }
        }

    }

    public interface ILabelResultCallBack {
        /**
         * 选中后的监听
         *
         * @param labelBean
         * @param filterDetailBeans 标签集合
         */
        void setOnclickReselutCallBack(BookLabelBean.FilterDetailBean labelBean, List<BookLabelBean.FilterDetailBean> filterDetailBeans);

        /**
         * 取消选中的监听
         *
         * @param filterDetailBeans 标签集合
         */
        void setOnclickCancellCallBack(BookLabelBean.FilterDetailBean labelBeanList, List<BookLabelBean.FilterDetailBean> filterDetailBeans);

        /**
         * 选中全部
         */
        void setOnclickAllCallBack();
    }
}
